package com.op.util;

import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import com.op.studyingliteapp.sender.Runtime;

/**
 * Created by Genji on 2015/6/28.
 */
public class ScreenSendUtils {

    public static void send(View view) {
        Runtime.setView(view);
        if (Runtime.isSend()&&!Runtime.isOffLine()) {
            new Thread(new Send(ScreenCapUtil.init(view))).start();
        }


    }
    public static void send() {
        if (Runtime.isSend()&&!Runtime.isOffLine()) {
            new Thread(new Send(ScreenCapUtil.init(Runtime.getView()))).start();
        }


    }
    public static void sendWithDely(View view) {
        Runtime.setView(view);
        if (Runtime.isSend()&&!Runtime.isOffLine()) {
            new Thread(new SendWithDely(view)).start();
        }


    }
    public static void send(ChannelHandlerContext context, Bitmap bitmap) {

        Log.i("test", "---send---start------------------");
        ByteBuf screencap ;
        byte[] bytes ;

        bytes = ScreenCapUtil.screenCapToByte(bitmap);
        if (bytes==null) {
            return;
        }
        screencap = Unpooled.buffer(bytes.length);
        Log.i("test", "bytebuf write int start");
        screencap.writeInt(bytes.length);
        Log.i("test", "bytebuf write int end");
        Log.i("test", "bytebuf write Bytes start");
        screencap.writeBytes(bytes);
        Log.i("test", "bytebuf write Bytes end");
        Log.i("test", "ChannelHandlerContext write ByteBuf start");
        context.writeAndFlush(screencap);
        Log.i("test", "ChannelHandlerContext write ByteBuf end");
        Log.i("test", "---send---done--length-----" + bytes.length + "-----------");
        Runtime.pause();
    }

//    public static void send(ChannelHandlerContext context, Bitmap bitmap) {
//        Log.i("test", "---send---start------------------");
//        ByteBuf screencap = null;
//        byte[] bytes = ScreenCapUtil.bitmapToByte(bitmap);
//        screencap = Unpooled.buffer(bytes.length);
//        Log.i("test", "bytebuf write int start");
//        screencap.writeInt(bytes.length);
//        Log.i("test", "bytebuf write int end");
//        Log.i("test", "bytebuf write Bytes start");
//        screencap.writeBytes(bytes);
//        Log.i("test", "bytebuf write Bytes end");
//        Log.i("test", "ChannelHandlerContext write ByteBuf start");
//        context.writeAndFlush(screencap);
//        Log.i("test", "ChannelHandlerContext write ByteBuf end");
//        Log.i("test", "---send---done--length-----" + bytes.length + "-----------");
//
//    }

    static class Send implements Runnable {
        Bitmap bitmap;

        public Send(Bitmap bitmap) {
            this.bitmap = bitmap;
        }

        @Override
        public void run() {
            ScreenSendUtils.send(Runtime.getContext(), bitmap);

        }

    }
    static class SendWithDely implements Runnable {
        private View view;

        public SendWithDely(View view) {

            this.view =view;
        }

        @Override
        public void run() {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            view.post(new Runnable() {
                @Override
                public void run() {
                    final Bitmap bitmap = ScreenCapUtil.init(view);
                    new Thread() {
                        @Override
                        public void run() {
                            ScreenSendUtils.send(Runtime.getContext(), bitmap);
                        }
                    }.start();


                }
            });


        }

    }
}
